package com.triones.api.service.interlace.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.triones.api.entity.KycVerifyDO;
import com.triones.api.entity.interlace.dto.InterlaceKycDO;
import com.triones.api.entity.interlace.vo.InterlaceKycVO;
import com.triones.api.entity.vo.KycVerifyVO;
import com.triones.api.mapper.interlace.InterlaceKycMapper;
import com.triones.api.service.interlace.KycService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * kyc 管理
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class KycServiceImpl implements KycService {

    private final InterlaceKycMapper interlaceKycMapper;

    @Override
    public TableDataInfo<InterlaceKycVO> pageList(InterlaceKycDO dto, PageQuery pageQuery) {
        Map<String, Object> params = dto.getParams();
        LambdaQueryWrapper<InterlaceKycDO> lqw = new LambdaQueryWrapper<InterlaceKycDO>();
        lqw.eq(StringUtils.isNotEmpty(dto.getStatus()), InterlaceKycDO::getStatus, dto.getStatus());
        lqw.eq(StringUtils.isNotEmpty(dto.getGender()), InterlaceKycDO::getGender, dto.getGender());
        lqw.and(StringUtils.isNotEmpty(dto.getFirstName()),
            qr -> qr.like(InterlaceKycDO::getFirstName, dto.getFirstName()).or()
                .like(InterlaceKycDO::getLastName, dto.getLastName())
        );
        lqw.like(StringUtils.isNotEmpty(dto.getNationalityName()), InterlaceKycDO::getNationalityName, dto.getNationalityName());
        lqw.and(StringUtils.isNotEmpty(dto.getAddressCountryName()),
            qr -> qr.like(InterlaceKycDO::getAddressCountryName, dto.getAddressCountryName()).or()
                .like(InterlaceKycDO::getAddressStateName, dto.getAddressCountryName()).or()
                .like(InterlaceKycDO::getCity, dto.getAddressCountryName()).or()
                .like(InterlaceKycDO::getAddressLine1, dto.getAddressCountryName()).or()
                .like(InterlaceKycDO::getAddressLine2, dto.getAddressCountryName())
        );
        lqw.like(StringUtils.isNotEmpty(dto.getPhoneNumber()), InterlaceKycDO::getPhoneNumber, dto.getPhoneNumber());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
            InterlaceKycDO::getCreateTime, params.get("beginTime"), params.get("endTime"));
        lqw.orderByDesc(InterlaceKycDO::getCreateTime);
        Page<InterlaceKycVO> result = interlaceKycMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public InterlaceKycVO getDetail(Long id) {
        return interlaceKycMapper.selectVoById(id);
    }
}
